package com.secondGroup.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.secondGroup.config.OperationNum;
import com.secondGroup.entity.ElectronicAccount;
import com.secondGroup.entity.Flow;
import com.secondGroup.entity.UserOperate;
import com.secondGroup.entity.VirtualBankCard;
import com.secondGroup.mapper.*;
import com.secondGroup.service.UserOperateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Service
public class UserOperateServiceImpl extends ServiceImpl<UserOperateMapper, UserOperate> implements UserOperateService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ElectronicAccountMapper electronicAccountMapper;
    @Autowired
    private VirtualBankCardMapper virtualBankCardMapper;
    @Autowired
    private FlowMapper flowMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String withdrawal(String account, BigDecimal eMoney,String ePayPassword, String cId) {
        //判断输入的密码是否为空或空字符串
        if (ePayPassword == null || ePayPassword.equals("")){
            return "密码输入不正确";
        }
        //根据用户账号查找到对应的电子账户id
        String eId = userMapper.selectEid(account);
        //这里查抄银行卡虚拟表银行卡id
        VirtualBankCard virtualBankCard = virtualBankCardMapper.selectVirtualBank(cId);
        //查找电子账户id
        ElectronicAccount electronicAccount = electronicAccountMapper.selectElectronic(eId);
        //判断提现金额是否小于0
        if(eMoney.compareTo(BigDecimal.ZERO) == -1){
            return "提现金额不正确";
        }
        //判断提现金额与电子账户余额作比较
        if (eMoney.compareTo(electronicAccount.getEMoney()) == 1){
            return "提现金额不正确";
        }
        //增加银行卡余额,并更改数据库数据
        virtualBankCard.setAbalance(virtualBankCard.getAbalance().add(eMoney));
        QueryWrapper<VirtualBankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_id", cId);
        int update1 = virtualBankCardMapper.update(virtualBankCard, queryWrapper);
        queryWrapper.clear();
        //减少电子银行卡余额,并更改数据库数据
        electronicAccount.setEMoney(electronicAccount.getEMoney().subtract(eMoney));
        QueryWrapper<ElectronicAccount> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("e_pay_password", ePayPassword).eq("account", account);
        int update2 = electronicAccountMapper.update(electronicAccount, queryWrapper1);
        //判断其中一个操作结果为0则为失败
        if (update1 == 0 || update2 == 0) {
            return "提现失败";
        }
        //增加提现流水到流水表
        Flow flow = new Flow();
        flow.setFId(IdWorker.getIdStr());
        flow.setTradeTime(LocalDateTime.now());
        flow.setTradeType(OperationNum.TiXian);
        flow.setTradeMoney(eMoney);
        flow.setAccount(account);
        flow.setEId(electronicAccount.getEId());
        flow.setIName(electronicAccount.getEName());
        flow.setTName(electronicAccount.getEName());
        flow.setTId(electronicAccount.getEId());
        flow.setIId(virtualBankCard.getCId());
        flow.setBalance(electronicAccount.getEMoney());
        int insert = flowMapper.insert(flow);
        if (insert == 0){
            return "提现失败";
        }
        return "提现成功";
    }

    @Override
    public String recharge(String account, BigDecimal eMoney, String payPassword, String cId) {
        //判断输入的密码是否为空或空字符串
        if (payPassword == null || payPassword.equals("")){
            return "密码输入不正确";
        }
        //根据用户账号查找到对应的电子账户id
        String eId = userMapper.selectEid(account);
        //查找银行卡虚拟表银行卡id
        VirtualBankCard virtualBankCard = virtualBankCardMapper.selectVirtualBank(cId);
        //查找电子账户id
        ElectronicAccount electronicAccount = electronicAccountMapper.selectElectronic(eId);
        //判断充值金额是否小于0
        if(eMoney.compareTo(BigDecimal.ZERO) == -1){
            return "充值金额不正确";
        }
        //判断充值金额与银行卡余额作比较如果小于
        if (eMoney.compareTo(virtualBankCard.getAbalance()) == 1){
            return "充值金额不正确";
        }
        //减少银行卡余额,并更改数据库数据
        virtualBankCard.setAbalance(virtualBankCard.getAbalance().subtract(eMoney));
        QueryWrapper<VirtualBankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pay_password", payPassword).eq("c_id", cId);
        int update1 = virtualBankCardMapper.update(virtualBankCard, queryWrapper);
        queryWrapper.clear();
        //增加电子银行卡余额,并更改数据库数据
        electronicAccount.setEMoney(electronicAccount.getEMoney().add(eMoney));
        QueryWrapper<ElectronicAccount> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("e_id", eId);
        int update2 = electronicAccountMapper.update(electronicAccount, queryWrapper1);
        //判断其中一个操作结果为0则为失败
        if (update1 == 0 || update2 == 0) {
            return "充值失败";
        }
        //增加充值流水到流水表
        Flow flow = new Flow();
        flow.setFId(IdWorker.getIdStr());
        flow.setTradeTime(LocalDateTime.now());
        flow.setTradeType(OperationNum.ChongZhi);
        flow.setTradeMoney(eMoney);
        flow.setAccount(account);
        flow.setEId(electronicAccount.getEId());
        flow.setTName(electronicAccount.getEName());
        flow.setIName(electronicAccount.getEName());
        flow.setIId(virtualBankCard.getCId());
        flow.setTId(electronicAccount.getEId());
        flow.setBalance(electronicAccount.getEMoney());
        int insert = flowMapper.insert(flow);
        if (insert == 0){
            return "充值失败";
        }
        return "充值成功";
    }
}
